/*
 * @Author: your name
 * @Date: 2021-11-01 15:58:05
 * @LastEditTime: 2022-01-14 14:30:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \star-cat-h5\src\vuex\store.js
 */
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import {
  getAdminInfoByCode
} from '../axios/api'
import getters from "./getters"
import artist from "./modules/artist"
import invoice from './invoice'
// 定义属性（数据）
var state = {
  urlParam: {},
  review_id: 0, //用户id
}

// 定义 getter
var getter = {

  getReview_id(state) {
    return state.review_id
  },
  getUrlParam(state) {
    return state.urlParam
  },

  getCode(url) {
    let param = new Object();
    let str = url.substr(1);
    let strs = str.split("&");
    for (let i = 0; i < strs.length; i++) {
      param[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
    return param;
  },
  // 是否登录
  getIsLogin(state) {
    return state.review_id == 0 ? false : true
  }
}

// 定义 actions ，要执行的动作，如流程的判断、异步请求
const actions = {
  getAdminInfoByCode({
    commit
  }) {
    let param = getter.getCode(window.location.search)
    if (process.env.VUE_APP_ENV == 'dev')
      return commit('mutationsReview_id', 10602)
    //   return commit('mutationsReview_id', 10472)
    return getAdminInfoByCode(param.code).then(
      (data) => {
        commit('mutationsReview_id', data.id)
        commit('mutationsUrlParam', param)
      },
      function (error) {}
    );
  }

}

// 定义 mutations ，处理状态（数据） 的改变
const mutations = {
  //与上方 commit 中的 ‘increment’ 相对应
  mutationsReview_id(state, data) {
    sessionStorage.setItem('review_id', data);
    state.review_id = data
  },
  mutationsUrlParam(state, data) {
    sessionStorage.setItem('urlParam', JSON.stringify(data));
    state.urlParam = data
  },
}
// 创建 store 对象
const store = new Vuex.Store({
  state,
  getters: {
    ...getter,
    ...getters
  },
  actions,
  mutations,
  modules: {
    artist,
    invoice
  }
})

// 导出 store 对象
export default store;